/*
 * FileList.java
 *
 * Created on 25 September 2006, 14:29
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.sghdc;

import libjdc.dc.filelist.File;
import hubsniffer.sghdc.file.io.FileListFlusher;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Comparator;
import java.util.TreeSet;



/**
 *
 * @author root
 */
public class FileList extends TreeSet<File>{
    
    public static final String FILE_SQL = "INSERT INTO \"file\" (\"file_name\",\"file_size\",\"file_TTH\",\"file_count\") VALUES (''{0}'',{1},''{2}'',{3})";
    public static final MessageFormat FILE_PARSER = new MessageFormat(FILE_SQL);
    private int buffer;
    private FileWriter out;
    private boolean buffered;
    private int numFlush;
    private static String DEFAULT_FILENAME = "/home/dbotelho/Filelist.txt";
    
    /** Creates a new instance of FileList */
    public FileList(int buffer) {
       this(FileList.DEFAULT_FILENAME,buffer);
    }
    
    /** Creates a new instance of FileList */
    public FileList(String defaultFilename,int buffer) {
       super(new NoFileDupesComparator());
       DEFAULT_FILENAME = defaultFilename;
       this.buffer = buffer;
       buffered= false;
       numFlush = 0;
    }
    
    public boolean add(File file){
        //System.out.println(this.size()+" "+buffer);
        if(this.size() > buffer){
            try {
                flushListToFIle();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        if(
                file.getName().endsWith(".mp3") ||
                file.getName().endsWith(".avi") ||
                file.getName().endsWith(".ogg") ||
                file.getName().endsWith(".rar") ||
                file.getName().endsWith(".iso") ||
                file.getName().endsWith(".mpeg") ||
                file.getName().endsWith(".pdf") ||
                file.getName().endsWith(".jpg")
                )
            return super.add(file);
        else
            return false;
    }
    
    public static void main(String[] args) {
        FileList f = new FileList("/home/dbotelho/Filelist2.txt",2);

        boolean buffered = false;
        int i=0;
        while(i<6){
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("b.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("b.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("b.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%CB5%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%C3%A5?%C2%A0%C3%A5??%C3%A6%C2%AF?%C3%A6%C2%B5%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%C3%A5ssssss%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%C3%A5?%C2%A0%ssssC3%A5??%C3%A6%C2%AF?%C3%A6%C2%B5%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%C3%A5bbbb?%C2%A0%C3%A5??%C3%A6%C2%AF?%C3%A6%C2%B5%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%CB5%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%C3%A5?%C2%A0%C3%A5??%C3%A6%C2%AF?%C3%A6%C2%B5%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%C3%A5ssssss%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%C3%A5?%C2%A0%ssssC3%A5??%C3%A6%C2%AF?%C3%A6%C2%B5%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("[%C3%A5bbbb?%C2%A0%C3%A5??%C3%A6%C2%AF?%C3%A6%C2%B5%C2%B7%C3%A7??].XZD.Pirates.of.the.Caribbean.The.Curse.of.the.Black.Pearl.disk3.srt.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("b.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("b.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("a.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            f.add(new File("b.mp3","20103","QR6HXGDE7BWN77S6ZOLMJKUTXMVQ5B5VV53SMWI"));
            try {
                f.flushListToFIle();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            i++;
        }
    }
    
    /**
     * private void flushListToFIle(String filename) throws FileNotFoundException, IOException {
        //throw new UnsupportedOperationException("Not yet implemented");
        if(out == null)
            try {
                out = new FileWriter(filename,true);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        //NoFileDupesComparator comp = new NoFileDupesComparator();
        BufferedReader in;
        String line;
        int dups=0;

        Iterator<File> it = this.iterator();
        System.out.println("Vai fazer flush para o ficheiro de "+this.size()+" entradas");
        while(it.hasNext()){
            File tmp = it.next();
            Object[] obj = { tmp.getName(),tmp.getSize(),tmp.getTTH(),1 };
            
            String nextFile = FILE_PARSER.format( obj );
            boolean append = true;
            in = new BufferedReader(new FileReader(filename));
            while((line = in.readLine())!= null && append){
                if(nextFile.equalsIgnoreCase(line)) { // quer dizer q passou os menores e pode entrar
                    //out.write(nextFile+"\n");
                    append = false;
                }
            }
            if(append)
                out.write(nextFile+"\n");
            else
                dups++;
            
            in.close();
        }
        System.out.println("Foram encontradas "+dups+" entradas repetidas");
        this.removeAll(this);
        try {
            out.close();
            out = null;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }*/

    public void flushListToFIle() throws FileNotFoundException, IOException {
        new FileListFlusher(this.iterator(),DEFAULT_FILENAME,buffered).flush();
        buffered = true;
        this.removeAll(this);
        //System.out.println("flushListToFIle"+this.size()+" "+buffer);
    }
    
    public void flushListToFIle(String filename) throws FileNotFoundException, IOException {
        DEFAULT_FILENAME = filename;
        flushListToFIle();
    }
}

class NoFileDupesComparator implements Comparator<File> {

	/**
	 * 
	 */
	public NoFileDupesComparator() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(hubsniffer.User, hubsniffer.User)
	 */
	public int compare(File arg0, File arg1) {
		// TODO Auto-generated method stub
		return (arg0.getName()+""+arg0.getSize()+""+arg0.getTTH()).compareTo(arg1.getName()+""+arg1.getSize()+""+arg1.getTTH());
	}

}
